package com.hilaver.dzmis.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 
 * 
 */
public class EncodingFilter implements javax.servlet.Filter {
	static final long serialVersionUID = 1L;
	
	private final static String ENCODING = "encoding";
	
	private final static String XML_RESPONSE_TYPE = "xmlResponseType";
	
	private String encoding;
	
	private String responseType;
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(this.encoding);
		response.setContentType(this.responseType);
		response.setCharacterEncoding(this.encoding);
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.encoding = config.getInitParameter(ENCODING);
		if(this.encoding == null) this.encoding="UTF-8";
		this.responseType = config.getInitParameter(XML_RESPONSE_TYPE);
		if(this.responseType == null) this.responseType = "text/xml;charset=utf-8";
	}

}